package com.novellotus.iov.carvice.view;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Rect;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.activity.AddOutGoingActivity;
import com.novellotus.iov.carvice.adapter.CarScreenAdapter;
import com.novellotus.iov.carvice.adapter.CarScreenItemAdapter;
import com.novellotus.iov.carvice.bean.RxVehiclesType;
import com.novellotus.iov.carvice.utils.MyUtils;
import com.novellotus.iov.carvice.utils.MyViewUtils;
import com.novellotus.iov.carvice.utils.TimeUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Rich on 2018/1/8.
 *
 */

public class CarScreenPopupWindow extends PopupWindow {

    private Context context;
    private List<RxVehiclesType> rxVehiclesTypeList;
    private CarScreenItemAdapter.CarScreenItemClickInterface carScreenItemClickInterface;
    private CarScreenPopupWindow.CarScreenPopupInterface carScreenPopupInterface;
    private int carStatuePosition;
    private int carModelPosition;
    private Calendar calendar = Calendar.getInstance();
    private boolean isOutGoging = false;//是否是出库状态
    private String startTime, endTime;

    public CarScreenPopupWindow(Context context, CarScreenItemAdapter.CarScreenItemClickInterface carScreenItemClickInterface, CarScreenPopupWindow.CarScreenPopupInterface carScreenPopupInterface, List<RxVehiclesType> rxVehiclesTypeList, int carStatuePosition, int carModelPosition, boolean isOutGoging, String startTime, String endTime) {
        this.context = context;
        this.rxVehiclesTypeList = rxVehiclesTypeList;
        this.carScreenItemClickInterface = carScreenItemClickInterface;
        this.carScreenPopupInterface = carScreenPopupInterface;
        this.carStatuePosition = carStatuePosition;
        this.carModelPosition = carModelPosition;
        this.isOutGoging = isOutGoging;
        this.startTime = startTime;
        this.endTime = endTime;
        if (this.rxVehiclesTypeList == null) {
            this.rxVehiclesTypeList = new ArrayList<>();
        }
        initPopupWindow();
    }


    private void initPopupWindow() {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_carscreen, null);
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        final TextView tv_begin = view.findViewById(R.id.tv_begin);
        final TextView tv_end = view.findViewById(R.id.tv_end);
        Button btn_cancel = view.findViewById(R.id.btn_reset);
        Button btn_search = view.findViewById(R.id.btn_search);

        if (!startTime.equals("")) {
            tv_begin.setText(startTime);
        }
        if (!endTime.equals("")) {
            tv_end.setText(endTime);
        }

        tv_begin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                chooseData(tv_begin, 0);
            }
        });
        tv_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                chooseData(tv_end, 1);
            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                carScreenPopupInterface.clickCancel();
            }
        });
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                carScreenPopupInterface.clickSearch();
            }
        });
        recyclerView.setLayoutManager(MyViewUtils.getLinearLayoutManager(context));
        MyUtils.logInformation(isOutGoging ? "新增" : "查看");
        recyclerView.setAdapter(new CarScreenAdapter(rxVehiclesTypeList, carScreenItemClickInterface, carStatuePosition, carModelPosition, isOutGoging));
        setOutsideTouchable(false);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_alpha_white));
        setContentView(view);
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT == 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if (Build.VERSION.SDK_INT == 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor, xoff, yoff);
    }


    /**
     * @param tv 选择时间
     *           type 0是开始时间 1是结束时间
     */
    void chooseData(final TextView tv, final int type) {
        final DatePicker date = new DatePicker(context);
        date.setCalendarViewShown(false);
        date.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            }
        });
        AlertDialog.Builder mDatePickerDialogBuilder = new AlertDialog.Builder(context);
        mDatePickerDialogBuilder.setView(date);
        mDatePickerDialogBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (System.currentTimeMillis() < TimeUtils.dateToStamps(date.getYear() + "-" + (date.getMonth() + 1) + "-" + date.getDayOfMonth())) {
                    MyUtils.showToast(context, "选择日期不能大于当前时间");
                } else {
                    //只有点击确定按钮时，才更改时间，并设置在文本中显示
                    int year = date.getYear();
                    int month = date.getMonth() + 1;
                    int day = date.getDayOfMonth();
                    String monthStr;
                    if (month < 10) {
                        monthStr = "0" + month;
                    } else {
                        monthStr = month + "";
                    }
                    String dayStr;
                    if (day < 10) {
                        dayStr = "0" + day;
                    } else {
                        dayStr = day + "";
                    }

                    String date = year + "-" + monthStr + "-" + dayStr;
                    tv.setText(date);
                    if (type == 0) {
                        carScreenPopupInterface.chooseStartTime(date);
                    } else {
                        carScreenPopupInterface.chooseEndTime(date);
                    }

                }

            }
        });
        mDatePickerDialogBuilder.setNegativeButton("取消", null);
        mDatePickerDialogBuilder.show();

    }


    public interface CarScreenPopupInterface {

        void clickCancel();

        void clickSearch();

        void chooseStartTime(String startTime);

        void chooseEndTime(String endTime);
    }
}
